<template>
  <div>
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.go(-1)" fixed></van-nav-bar>
    <div class="line"></div>
    <van-cell is-link>
      <template #title>
        <div class="textImg">
          <div class="text">头像</div>
          <van-image class="img" fit="cover" round :src="userObj.photo" @click="imgfn()" />
          <!-- 阻止非图片,不需要展示只需要调用方法 -->
          <input type="file" accept="image/*" ref="iptFile" name="" id="" v-show="false" @change="onFileChnage">
        </div>
      </template>
    </van-cell>
    <van-cell is-link @click="nameFn()">
      <template #title>
        <div class="textImg">
          <div class="text">名称</div>
          <div>{{ userObj.name }}</div>
        </div>
      </template>
    </van-cell>
    <van-cell is-link @click="show2 = true">
      <template #title>
        <div class="textImg">
          <div class="text">生日</div>
          <div>{{ userObj.birthday }}</div>
        </div>
      </template>
    </van-cell>

    <van-dialog v-model="show" title="姓名修改" show-cancel-button :before-close="closeFn">
      <input type="text" v-fofo v-model.trim="userName" maxlength="7">
    </van-dialog>

    <van-popup v-model="show2" position="bottom" round :style="{ height: '50%' }">
      <van-datetime-picker v-model="currentDate" @cancel="cancel" @confirm="confirm" type="date" title="选择年月日" :min-date="minDate" :max-date="maxDate" />
    </van-popup>

  </div>
</template>

<script>
import Notify from '@/ui/Notify'
import { mapMutations } from 'vuex'
export default {
  name: 'edit',
  created () {
    this.getData()
  },
  data () {
    return {
      userObj: {},
      show: false,
      show2: false,
      showimg: false,
      userName: '',
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: new Date()
    }
  },
  methods: {
    ...mapMutations(['SET_USERPHOTO', 'SET_USERNAME']),
    async getData () {
      const { data: res } = await this.$api.userprofileApi()
      this.userObj = res.data
      this.currentDate = new Date(this.userObj.birthday)
    },
    // 点击图片然后模拟上传框的点击事件
    imgfn () {
      this.$refs.iptFile.click()
    },
    async onFileChnage (e) {
      if (e.target.files.length === 0) {
        return
      }
      // 创建一个formdata对象
      const files = new FormData()
      // 增加一个photo字段,参数为files
      files.append('photo', e.target.files[0])
      const { data: res } = await this.$api.userphotoeditAPI(files)
      this.userObj.photo = res.data.photo
      this.SET_USERPHOTO(res.data.photo)
    },
    nameFn () {
      this.show = true
    },
    closeFn (action, done) {
      if (action === 'confirm') {
        // 校验1-7位的中英文数字组合
        const reg = /^[a-zA-Z0-9\u4e00-\u9fa5]{1,7}$/
        // 调用正则表达式中的方法测试是否符合条件
        if (reg.test(this.userName) === true) {
          this.$api.edituserprofileAPI({ name: this.userName })
          this.userObj.name = this.userName
          // eslint-disable-next-line semi
          this.SET_USERNAME(this.userName);
          done()
        } else {
          Notify({ type: 'warning', message: '用户名只能是1-7位中英文的组合' })
          // 阻止弹窗关闭
          this.userName = ''
          done(false)
        }
      } else {
        done() // 调用关闭方法
      }
    },
    cancel () {
      this.show2 = false
    },
    confirm () {
      this.$api.edituserprofileAPI({ birthday: this.$utils.formatDate(this.currentDate) })
      this.userObj.birthday = this.$utils.formatDate(this.currentDate)
      this.show2 = false
    }
  }
}
</script>

<style lang="less" scoped>
.van-cell {
  align-items: center;
}

/deep/ .van-icon-arrow-left {
  color: #fff;
  font-size: 18px;
}

/deep/ .van-nav-bar__content {
  height: 54px;
}

.line {
  margin-bottom: 54px;
}

input {
  border: none;
  text-align: center;
}

.img {
  width: 50px;
  height: 50px;
}

.textImg {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.text {
  text-align: left;
}
</style>
